package com.wn.purchaeReturn.controller;

import com.wn.purchaeReturn.entity.PurPurchasereturn;
import com.wn.purchaeReturn.service.PurchaseService;
import com.woniu.common.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Frank
 * @Date: 2022/4/18  15:07
 * @Description:
 */
@RestController
@Slf4j
@RequestMapping("/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 展示所有退货信息
     * @return
     */
    @GetMapping("/selAll")
    public Result selAll(){
        Result result=new Result();
        List<PurPurchasereturn> list = purchaseService.selAllPurcha();
        if(list!=null&&list.size()>0){
            result.setCode(1000);
            result.setMessage("查询成功");
            result.data("list",list);
        }else {
            result.setCode(1001);
            result.setMessage("查询异常");
        }
        System.out.println("selAllPur:"+list);
        return  result;

    }

    /**\
     * 通过退货单单号进行查询
     * @param PurchaseRtnNo
     * @return
     */
    @GetMapping("/selById/{PurchaseRtnNo}")
    public Result selById(@PathVariable("PurchaseRtnNo") Integer PurchaseRtnNo){
        log.info("PurchaseRtnNo::",PurchaseRtnNo);
        Result result=new Result();
        PurPurchasereturn pur=purchaseService.selById(PurchaseRtnNo);
        if (pur!=null){
            result.setMessage("查询成功");
            result.data("pur",pur);
        }else {
            result.setMessage("查询异常");
        }
        return result;
    }

    /**
     * 增加采购单
     * @param purPurchasereturn
     * @return
     */

    @PostMapping("/addpurchase")
    public Result addpurchase(@RequestBody PurPurchasereturn purPurchasereturn){
        Result result=new Result();
        int count = purchaseService.insert(purPurchasereturn);
        if(count > 0){
            result.setCode(1000);
            result.setMessage("添加成功");
            result.setData1(purPurchasereturn);
        }else {
            result.setCode(1001);
            result.setMessage("添加失败");
        }
        return  result;

    }

    /**
     * 修改采购单
     * @param purPurchasereturn
     * @return
     */
    @PutMapping("/uppurchase")
    public Result uppurchase(@RequestBody PurPurchasereturn purPurchasereturn){
        Result result=new Result();
        System.out.println(purPurchasereturn);
        int count = purchaseService.updateByPrimaryKey(purPurchasereturn);
        if(count > 0){
            result.setCode(1000);
            result.setMessage("修改成功");
            result.setData1(purPurchasereturn);
        }else {
            result.setCode(1001);
            result.setMessage("修改失败");
        }
        return  result;

    }

}
